package com.collectplus.express.self;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.app.AppCaptureActivity;
import com.collectplus.express.logic.AppCache;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.BagBean;
import com.collectplus.express.model.Const.RequestCode;
import com.collectplus.express.model.ReceiverBean;
import com.collectplus.express.tools.SharedPref;
import com.shouhuobao.bhi.receiver.ReceiverCameraActivity;
import com.shouhuobao.bhi.receiver.ReceiverTabFragmentActivity;

import droid.frame.activity.title.TitleRes;
import droid.frame.umeng.UMeng;

/**
 * 自助寄件首页
 *
 * @author coffee <br>
 *         2016-9-24上午10:45:54
 */
public class SelfMainActivity extends BaseActivity2 implements OnClickListener {

    @Override
    @SuppressLint("ResourceAsColor")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (SharedPref.isFirstOpen(3)) {
            Intent intent = new Intent();
            intent.putExtra("type", 3);
            intent.setClass(getContext(), SelfUserPassportActivity.class);
            startActivity(intent);
        }
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.self_main);
        super.findViewById();
        getAppTitle().setCommonTitle("投递寄件", new TitleRes(R.drawable.menu_feedback, new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), SelfUserPassportActivity.class));
            }
        }));
        findViewById(R.id.self_hasbag).setOnClickListener(this);
        findViewById(R.id.self_hasNobag).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            // 有投递袋
            case R.id.self_hasbag:
                UMeng.click(getContext(), "selfbag_scan_click");
                intent = new Intent();
                intent.setClass(getContext(), AppCaptureActivity.class);
                startActivityForResult(intent, RequestCode.scan_bag);
                break;
            // 我还没有投递袋,没有投递袋子跳转到填写包裹信息界面
            case R.id.self_hasNobag:
                UMeng.click(getContext(), "selfbag_noBag_click");
                //			Intent intent = new Intent();
                //			intent.setClass(getContext(), WebViewActivity.class);
                //			intent.putExtra("title", "如何获取投递袋");
                //			startActivityForResult(intent, 102);
                intent = new Intent();
                intent.setClass(getContext(), SelfFormInfoActivity.class);
                intent.putExtra("bag", new BagBean());
                startActivityForResult(intent, 102);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.order_goHome_avaliable:
                cancelLoadingDialog();
                AppResult<?> result1 = JSON.parser(msg.obj);
                if (result1.isSuccess()) {
                    startGoHome();
                } else {
                    showToast(result1.getMessage());
                }
                return true;
            // 检测投递袋是否合法
            // case MsgID.self_bagcode_check:
            // cancelLoadingDialog();
            // AppResult<BagBean> result = JSON.parser(msg.obj, BagBean.class);
            // if (result.isSuccess() && result.getResult() != null) {
            // Intent intent = new Intent();
            // intent.setClass(getContext(), SelfFormInfoActivity.class);
            // intent.putExtra("bag", result.getResult());
            // startActivity(intent);
            // } else {
            // showToast(result.getMessage());
            // }
            // return false;
            default:
                return super.handleMessage(msg);
        }
    }

    private void startGoHome() {
        Intent intent = new Intent();
        intent.setClass(getContext(), ReceiverCameraActivity.class);
        intent.putExtra("type", "0");
        startActivityForResult(intent, 103);
        if (SharedPref.isFirstOpen(4)) {
            intent = new Intent();
            intent.putExtra("type", 4);
            intent.setClass(getContext(), SelfUserPassportActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 获取投递袋、超链接回调 -- 转为标准寄件
        if (requestCode == 102 && resultCode == RESULT_OK) {
            // 如果已经登录则判断是否有未完成的订单
            if (isLogin()) {
                showLoadingDialog(null);
                AppHttp.getInstance().orderGoHomeAvaliable();
            } else {
                startGoHome();
            }
        }
        // 标准寄件、拍照
        else if (requestCode == 103 && resultCode == RESULT_OK) {
            String filePath = data.getStringExtra("file");
            ReceiverBean item = AppCache.getParcelNew();
            if (item == null) {
                item = new ReceiverBean();
                AppCache.setParcelNew(item);
            } else {
                item.setSender(null);// 上门的没有寄件人信息
            }
            item.setType(0);// 标准寄件
            item.setGoodsImage(filePath);
            Intent intent = new Intent();
            intent.setClass(getContext(), ReceiverTabFragmentActivity.class);
            startActivityForResult(intent, 104);
        }
        // 标准寄件 收件人地址
        else if (requestCode == 104 && resultCode == RESULT_OK) {

        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
